Skip to content

fix(error-handling-middleware): handle str error as ApiError #287

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 17 commits into from
Apr 5, 2025

Conversation

naorpeled
Copy link
Collaborator

@naorpeled naorpeled commented Apr 4, 2025

Description

Before, when using res.error(message), the message would be sent as string and not as an Error object to the error handling middleware.

This PR adds a new error type called ApiError and this error will be sent to the error handling middleware.

Issue

Fixes #286

@naorpeled naorpeled requested a review from Copilot April 4, 2025 20:09
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.

Comments suppressed due to low confidence (2)

lib/response.js:607

  • Consider ensuring that 'errorToSend' is always an instance of Error before passing it to 'app.catchErrors' to maintain consistent error handling.
this.app.catchErrors(errorToSend, this, statusCode, errorDetail);

index.js:360

  • [nitpick] The variable name 'wasStringError' may be ambiguous; consider renaming it to something more descriptive like 'isResponseErrorFromString' to improve clarity.
const wasStringError = e instanceof ResponseError && e.originalMessage !== undefined;

@naorpeled naorpeled changed the title fix/error handling/treat string err as regular err fix(error-handling-middleware): handle str error as ApiError Apr 5, 2025
@naorpeled naorpeled requested a review from Copilot April 5, 2025 17:15
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot reviewed 7 out of 7 changed files in this pull request and generated no comments.

Comments suppressed due to low confidence (3)

lib/response.js:595

  • The logic for assigning message, statusCode, and errorDetail is non-intuitive. Please add inline comments to clarify how the conditions distinguish between a string error and an error object.
error(code, e, detail) {

lib/response.js:603

  • Only converting errors with a string message to ApiError may lead to unexpected handling of non-string errors. Verify that this conditional behavior is intentional and covers all error cases.
const errorToSend = typeof message === 'string' ? new ApiError(message, statusCode, errorDetail) : message;

index.js:355

  • ApiError instances are logged in a different branch from other Error instances. Please confirm that this distinction in logging levels is intentional for proper error categorization.
const isApiError = e instanceof ApiError;

@naorpeled naorpeled marked this pull request as ready for review April 5, 2025 17:21
@naorpeled naorpeled merged commit 6de4177 into main Apr 5, 2025
7 checks passed
@naorpeled naorpeled deleted the fix/error-handling/treat-string-err-as-regular-err branch April 5, 2025 17:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

The type of the error argument of ErrorHandlingMiddleware should be Error or string
1 participant